class ExamRoom
{
public:
    ExamRoom(int n)
    {
        this->n = n;
    }

    int seat()
    {
        if (this->occupiedSeats.empty())
        {
            this->occupiedSeats.insert(0);
            return 0;
        }

        int maxDistance = 0;
        int maxDistanceSeat = 0;
        auto it = this->occupiedSeats.begin();
        if (*it > maxDistance)
        {
            maxDistance = *it;
            maxDistanceSeat = 0;
        }
        auto lastIt = --this->occupiedSeats.end();
        auto nextIt = it;
        for (++nextIt; it != lastIt; ++it, ++nextIt)
        {
            int distance = (*nextIt - *it) / 2;
            if (distance > maxDistance)
            {
                maxDistance = distance;
                maxDistanceSeat = *it + distance;
            }
        }
        if (this->n - 1 - *lastIt > maxDistance)
        {
            this->occupiedSeats.insert(this->n - 1);
            return this->n - 1;
        }
        this->occupiedSeats.insert(maxDistanceSeat);
        return maxDistanceSeat;
    }

    void leave(int p)
    {
        this->occupiedSeats.erase(p);
    }

private:
    set<int> occupiedSeats;
    int n;
};

/**
 * Your ExamRoom object will be instantiated and called as such:
 * ExamRoom* obj = new ExamRoom(n);
 * int param_1 = obj->seat();
 * obj->leave(p);
 */